package com.atguigu.controller;

import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.entity.Result;
import com.atguigu.utils.RedisMessageConstant;
import com.atguigu.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * ValidateCodeController
 *
 * @Author: 马伟奇
 * @CreateTime: 2021-02-02
 * @Description:
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        // 生成动态验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);

        System.out.println("发送短信验证码---" + code);

        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN,
                5 * 60,code + "");

        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }



    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //  生成手机验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        // 发送短信
        System.out.println("code===" + code);

        // 把发送到用户手机上面的验证码存到数据库
        // 设置过期时间
        jedisPool.getResource().setex(
                telephone + RedisMessageConstant.SENDTYPE_ORDER,
                5*60,
                code + "");


        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

    }
}